package com.wwl.open.controller;

import com.wwl.open.common.Result;
import com.wwl.open.entity.Monitor;
import com.wwl.open.service.MonitorService;
import com.wwl.open.util.ResourceMonitorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

@RestController
public class MonitorController {

    @Autowired
    private MonitorService monitorService;

    @GetMapping("/all")
    public List<Monitor> getAllMonitors() {
        return monitorService.getAllMonitors();
    }
    @GetMapping("/")
    public ModelAndView dashboard() {
        return new ModelAndView("monitor/dashboard.html");
    }
    @GetMapping("/real-time")
    public ModelAndView monitor() {
        return new ModelAndView("monitor/real-time.html");
    }
    @GetMapping("/latest")
    public Result latest() {
        Monitor monitor = ResourceMonitorUtil.getLatestMonitor();
        return Result.success(monitor);
    }
    @GetMapping("/history")
    public ModelAndView history() {
        return new ModelAndView("monitor/history.html");
    }
    @GetMapping("/historical")
    public Result getHistoricalData(@RequestParam(defaultValue = "1") int minutes) {
        LocalDateTime startDate = LocalDateTime.now().minus(minutes, ChronoUnit.MINUTES);
        List<Monitor> historicalData = monitorService.getHistoricalData(startDate);
        return Result.success(historicalData);
    }

}